/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.prefabware.commons.Condition;
import com.prefabware.commons.QualifiedName;
import com.prefabware.commons.StringUtil;
import com.prefabware.jmodel.reference.JReference;
import com.prefabware.jmodel.reference.JReferenceBase;

public abstract class JDeclarationBase implements JDeclaration,Cloneable {

	private JType declaringType;
	protected JModifiers modifiers;
	protected JVisibility visibility;
	protected List<JType> typeArguments;

	public void setTypeArguments(List<JType> typeArguments) {
		this.typeArguments = typeArguments;
	}

	public JDeclarationBase(QualifiedName qualifiedName, JModifiers modifiers, JVisibility visibility) {
		super();
		// constructors have name=null
		// Condition.hasLength(name, "..name");
		Condition.notNull(modifiers, "..modifiers");
		Condition.notNull(visibility, "..visibility");
		this.qualifiedName = qualifiedName;
		this.modifiers = modifiers;
		this.visibility = visibility;
		this.typeArguments = Collections.emptyList();
	}

	public JDeclarationBase clone() {
		JDeclarationBase cloned;
		try {
			cloned = (JDeclarationBase) super.clone();
			// create another list to allow for later modification
			cloned.setTypeArguments(new ArrayList<JType>(typeArguments));
			return cloned;
		} catch (CloneNotSupportedException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @return
	 */
	@Override
	public JVisibility getVisibility() {
		return visibility;
	}

	/**
	 * @return
	 */
	@Override
	public String getName() {
		return this.getQualifiedName().getSimpleName();
	}

	protected QualifiedName qualifiedName;

	public QualifiedName getQualifiedName() {
		return qualifiedName;
	}

	/**
	 * @return
	 */
	@Override
	public boolean isStatic() {
		return this.modifiers.contains(JModifier.STATIC);
	}

	/**
	 * @return
	 */
	@Override
	public JModifiers getModifiers() {
		return modifiers;
	}

	/**
	 * @return
	 */
	@Override
	public String getVisibilityAndModifier() {
		String result = this.visibility.toString() + " "
				+ StringUtil.getListAsString(this.modifiers.getModifiers(), " ");
		return result.trim();
	}

	/**
	 * @return
	 */
	@Override
	public JType getDeclaringType() {
		return declaringType;
	}

	/**
	 * @param declaringType
	 */
	@Override
	public void setDeclaringType(JType declaringType) {
		this.declaringType = declaringType;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((declaringType == null) ? 0 : declaringType.hashCode());
		result = prime * result + ((qualifiedName == null) ? 0 : qualifiedName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		JDeclarationBase other = (JDeclarationBase) obj;
		if (declaringType == null) {
			if (other.declaringType != null)
				return false;
		} else if (!declaringType.equals(other.declaringType))
			return false;
		if (qualifiedName == null) {
			if (other.qualifiedName != null)
				return false;
		} else if (!qualifiedName.equals(other.qualifiedName))
			return false;
		return true;
	}

	public List<JReference<?>> getTypeArgumentReferences() {
		List<JReference<?>> references = new ArrayList<JReference<?>>();
		for (JType refType : this.getTypeArguments()) {
			references.add(JReferenceBase.create(this.getDeclaringType(), refType, refType));
		}
		return references;
	}

	@Override
	public String toString() {
		return this.getClass().getSimpleName() + " [name=" + qualifiedName + ", declaringType=" + declaringType + "]";
	}

	public List<JType> getTypeArguments() {
		return typeArguments;
	}
}
